import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import User from '../views/User.vue'
import Edit from '../views/Edit.vue'
import MyAttention from '../views/MyAttention.vue'
import MyComments from '../views/MyComments.vue'
import ArticleDetails from '../views/ArticleDetails'
import MyCollection from '../views/MyCollection'
import Manage from '../views/Manage'
import Search from '../views/Search'
import GoodComments from '../views/GoodComments'
import test1 from '../views/test1'

Vue.use(VueRouter)




const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/test1',
    component: test1
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/user',
    component: User,
    meta: {
      needAuth: true
    }
  },
  {
    path: '/edit/:id',
    component: Edit,
    meta: {
      needAuth: true
    }
  },
  {
    path: '/myAttention/:id',
    component: MyAttention,
    meta: {
      needAuth: true
    }
  },
  {
    path: '/mycomments/:id',
    component: MyComments,
    meta: {
      needAuth: true
    }
  },
  {
    path: '/articleDetails/:id',
    component: ArticleDetails
  },
  {
    path: '/mycollection',
    component: MyCollection,
    meta: {
      needAuth: true
    }
  },
  {
    path: '/manage',
    component: Manage,
    meta: {
      needAuth: true
    }
  },
  {
    path: '/search',
    component: Search
  },
  {
    path: '/goodcomment/:id',
    component: GoodComments
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const hasToken = localStorage.getItem("token")
  // console.log(to.meta);
  if (to.meta.needAuth) {
    if (hasToken) {
      return next()
    } else {
      return router.push('/login').catch(err => { })
    }
  } else {
    next()
  }
})



export default router
